﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LiuQiuRong.Chat.Utilities.Crypto
{
    public class Decryption
    {
        /// <summary>
        /// 默认密钥向量
        /// </summary>
        private static readonly byte[] Keys = { 0x45, 0x34, 0xcd, 0xab, 0x90, 0xef, 0x23, 0x89 };

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch (Exception ex)
            {
                throw new Exception("DecryptDES Error", ex);
            }
        }

        /// <summary>
        /// AES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string DecryptAES(string decryptString, string decryptKey)
        {
            try
            {
                // byte[] inputByteArray = Encoding.UTF8.GetBytes(decryptString);
                byte[] inputByteArray = System.Convert.FromBase64String(decryptString);
                AesManaged aes = new AesManaged();
                Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(decryptKey, Keys);
                aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
                aes.KeySize = aes.LegalKeySizes[0].MaxSize;
                aes.Key = rfc.GetBytes(aes.KeySize / 8);
                aes.IV = rfc.GetBytes(aes.BlockSize / 8);

                // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象   
                ICryptoTransform decryptTransform = aes.CreateDecryptor();
                // 解密后的输出流   
                MemoryStream decryptStream = new MemoryStream();
                // 将解密后的目标流（decryptStream）与解密转换（decryptTransform）相连接   
                CryptoStream decryptor = new CryptoStream(decryptStream, decryptTransform, CryptoStreamMode.Write);
                // 将一个字节序列写入当前 CryptoStream （完成解密的过程）   
                decryptor.Write(inputByteArray, 0, inputByteArray.Length);
                decryptor.Close();
                // 将解密后所得到的流转换为字符串   
                byte[] decryptBytes = decryptStream.ToArray();
                return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
            }
            catch (Exception ex)
            {
                throw new Exception("DecryptAES Error", ex);
            }
        }

        /// <summary>
        /// Base解密字符串
        /// 需去掉前8位假数据
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static string DecryptBase64(string decryptString)
        {
            decryptString = decryptString.Remove(0, 8);
            string ret = Encoding.Unicode.GetString(Convert.FromBase64String(decryptString));
            return ret;
        }

    }
}
